import { Component } from '@angular/core';
import { NavController, NavParams, ViewController } from 'ionic-angular';
import { Api } from '../../../providers/api-service';
import { ImgService } from '../../../providers/camera-service';

/**
 * 打卡
 */
@Component({
  selector: 'page-clock',
  templateUrl: 'clock.html',
})
export class ClockPage {

  constructor(
    public navCtrl: NavController,
    public api: Api,
    public viewCtrl: ViewController,
    public imgService: ImgService,
    public navParams: NavParams) {
    this.GetTodayDaKaInfo();
  }

  ionViewDidLoad() {
    console.log('打卡 ClockPage');
  }

  IsReward=true;
  /**
  * 关闭页面
  */
  dismiss() {
    this.viewCtrl.dismiss();
  }

  DaKaInfoList = [];

  /**
   * 挑战集合
   */
  GetTodayDaKaInfo() {
    this.api.postBody("V1/GetTodayDaKaInfo", {})
      .then(data => {
        if (data.Code == 0) {
          this.DaKaInfoList = data.Data;
        }
      })
  }

  /**
   * 提交数据
   */
  DaKa = {
    "Describe": "",
    "VideoUrl": "",
    "IsOpen": true,
    "GroupId": [],
    "PicList": ["assets/imgs/img/uploadPic.png"]
  }


  /**
   * 打卡
   */
  nextBtn1() {

    if (this.isok(this.DaKa.Describe, "内容不能为空")) {
      if (this.DaKa.GroupId.length == 0) {
        this.api.showNoticeByToast(1, "请勾选挑战项目");
        return false;
      }
    }
    this.DaKa.IsOpen = this.IsReward;
    this.api.postBody("V1/GetTodayDaKaInfo", this.DaKa)
      .then(data => {
        if (data.Code == 0) { //打卡成功
          this.api.showNoticeByToast(1, data.Mag);
          setTimeout(() => {
            this.dismiss();
          }, 2000);
        }
      })
  }



  isok(val, msg) {
    if (val == "") {
      this.api.showNoticeByToast(1, msg);
    } else {
      return true;
    }
    return false;
  }

  /**
   * 选择事件
   * @param event x
   * @param item 
   */
  updateCucumber(event, item) {
    if (event.checked) {
      this.DaKa.GroupId.push(item);
    } else {
      this.DaKa.GroupId.splice(item, 1);
    }
  }
  /**
   * 上传图片
   */
  upPic() {
    this.imgService.showPicActionSheet("Dynamic", data => {
      let msg = eval("(" + data["response"] + ")");

      this.DaKa.PicList[0] = msg.Data;//图片地址
    })
  }



  /**
   * 上传视频
   */
  upVideo() {
    
    this.imgService.startMedia((data)=>{
      alert(data);
    });
  }
}
